python regex flags

40

re.A | re.ASCII			Perform ASCII-only matching instead of full Unicode matching

re.I | re.IGNORECASE	Perform case-insensitive matching

re.M | re.MULTILINE		^ matches the pattern at beginning of the string and each newline’s beginning (\n).
               			$ matches pattern at the end of the string and the end of each new line (\n)

re.S | re.DOTALL		Without this flag, DOT(.) will match anything except a newline

re.X | re.VERBOSE 		Allow comment in the regex

re.L | re.LOCALE		Perform case-insensitive matching dependent on the current locale. Use only with bytes patterns
import re
#	Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods, described below.

prog = re.compile(pattern)
result = prog.match(string)

#	is equivalent to

result = re.match(pattern, string)
import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']

Comments

Submit
0 Comments